/*global Qiniu */
/*global plupload */
/*global FileProgress */
/*global hljs */

$(function() {
    var uploader = Qiniu.uploader({
        runtimes: 'html5,flash,html4',
        browse_button: 'pickfiles',
        dragdrop: true,
        chunk_size: '4mb',
        // uptoken_url: $('#uptoken_url').val(),
        uptoken:"ZeBcNA5xgTjr75B8ALgfS1fjtadwEdS1l0ivQDOx:iz8jHpUXfJ1fDU4VQ-Ur7mH2_BU=:eyJzY29wZSI6IjNjY2ciLCJkZWFkbGluZSI6MTUyMzM2NDMzNX0=",
        filters : {
            max_file_size : '100000mb',
            prevent_duplicates: true,
            mime_types: [
            ]
        },
        domain: 'z2',
        get_new_uptoken: false,
        unique_names: false,
        auto_start: true,
        log_level: 5,
        init: {
            'FilesAdded': function(up, files) {
                $('table').show();
                $('#success').hide();
                plupload.each(files, function(file) {
                    var progress = new FileProgress(file, 'fsUploadProgress');
                    progress.setStatus("等待...");
                    progress.bindUploadCancel(up);
                });
            },
            'BeforeUpload': function(up, file) {
                var progress = new FileProgress(file, 'fsUploadProgress');
                progress.file.name = 'a'+ progress.file.name;
                var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
                if (up.runtime === 'html5' && chunk_size) {
                    progress.setChunkProgess(chunk_size);
                }
            },
            'UploadProgress': function(up, file) {
                var progress = new FileProgress(file, 'fsUploadProgress');
                var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
                progress.setProgress(file.percent + "%", file.speed, chunk_size);
            },
            'UploadComplete': function(res) {
                console.log(res);
                $('#success').show();
            },
            'FileUploaded': function(up, file, info) {

                var progress = new FileProgress(file, 'fsUploadProgress');
                progress.setComplete(up, info);
            },
            'Error': function(up, err, errTip) {
                $('table').show();
                var progress = new FileProgress(err.file, 'fsUploadProgress');
                progress.setError();
                progress.setStatus(errTip);
            }
            ,
            'Key': function(up, files) {
                var key = "abcd.jpg";
                return key
            }
        }
    });
});
